package com.wise.service.cms.content.article;

import com.wise.entities.content.Article;
import com.wise.entities.content.ArticleColumn;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoOne;
import com.wise.framework.daomodel.DaoPageList;
import com.wise.framework.restful.PageResultList;
import com.wise.summer.v1.cms.content.articles.ListArticles;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import static com.wise.model.content.ContentErrMsgs.Err_001600250014;

@Service
@SummerService
public class ListArticlesService implements SummerServiceBean<ListArticles> {
    @Override
    public void sum(ListArticles model) {

        SqlSelect sql = SqlSelect.selec("*")
                .fromClassName(Article.class)
                .like(!StringUtils.isEmpty(model.getTitle()), Article._title, "%" + model.getTitle() + "%")
                .eq(!StringUtils.isEmpty(model.getColumnId()), Article._columnId, model.getColumnId())
                .eq(model.getState() != null, Article._state, model.getState())
                .eq(model.getType() != null, "type", model.getType())
                .eqLang(Article._lang)
                .order_by("id desc")
                .order_by(Article._sort);

        PageResultList<Article> pageResultList = DaoPageList.s(Article.class, sql, model.getStart(), model.getLimit());


//        String columnName = DaoOne.s(ArticleColumn.class, article.getColumnId(), Err_001600250014).getColumnName();
//        pageResultList.getList().forEach(article -> {
//
//            article.setColumnName(columnName);
//        });

        model.setSummerResult(pageResultList);
    }

}
